package com.itheima.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entry.PageResult;
import com.itheima.entry.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;

/**
 * @author jshstart
 * @creat 2020-11-10 10:39
 */
@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {
//    @Override
//    public boolean deleteById(CheckItem checkItem) {
//        checkItem.setIs_delete(1);
//        return this.updateById(checkItem);
//
//    }

    @Override
    public List<CheckItem> findAllItem() {


        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        return this.list(wrapper);
    }

    @Override
    public boolean deleteById(Long id) {
        CheckItem checkItem = baseMapper.selectById(id);
        if (checkItem!=null) {
            checkItem.setIs_delete(1);
        }
        boolean b = this.updateById(checkItem);
        return b;
    }

    @Override
    public PageResult findAllByPage(QueryPageBean queryPageBean) {

        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<CheckItem> page=null;
        if (StringUtils.isEmpty(queryPageBean.getQueryString())){
             page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }else {
            wrapper.like("name",queryPageBean.getQueryString());
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }
        PageResult result = new PageResult(page.getTotal(), page.getRecords());
        return result;
//        IPage<CheckItem> page =this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
//        //IPage<CheckItem> page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()));
//        PageResult result = new PageResult(page.getTotal(), page.getRecords());
//        return result;
    }
//select tci.* from t_checkgroup_checkitem tgi
//join t_check_item tci on tci.id=tgi.checkitem_id
//where tgi.group_id=36
    @Override
    public List<CheckItem> findAllItemByGroupId(Integer id) {

        return baseMapper.findAllItemByGroupId(id);

    }
}
